package com.bookcloud.java21.chapter9.写入字符流文件94;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/** 写入字符流文件 */
public class WriteCharacterFileExample {
  public static void main(String[] args) {
    // 一、通过BufferedWriter写入文件
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\mydata.txt", true))) {
      bw.newLine();
      bw.write("这是第一行");
      bw.newLine();
      bw.write("这是第二行");
      bw.flush(); // 刷新缓冲区
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 二、PrintWriter写入文件
    try (PrintWriter pw = new PrintWriter(new FileWriter("d:\\mydata.txt", true))) {
      pw.write("\n");
      pw.println("这是第三行");
      pw.println("这是第四行");
      pw.flush(); // 刷新缓冲区
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
